(function($) {

	function DataForm() {
	}
	var openWindow = function(config) {
		if(config.max){
			mini.open({
				url : config.url,
				title : config.title,
				width : config.width,
				height : config.height,
				showMaxButton: config.showMaxButton,
				onload : function() {
					if (config.action == "update") {
						var iframe = this.getIFrameEl();
						iframe.contentWindow.$.form.showData(config);
					}
				},
				ondestroy : function(action) {
					if (action&&action.indexOf("_") > 0) {
						var actions = action.split("_");
						var actionName = actions[0];
						var msg = actions[1];
						if (actionName == "save" && msg != "null" && msg != "") {
							tips.show(msg, '', 5000, '#DFF0D8', 150);
						}
					}
				}
			}).max ();//最大化
		}else{
			mini.open({
				url :  config.url,
				title : config.title,
				width : config.width,
				height : config.height,
				showMaxButton: config.showMaxButton,
				onload : function() {
					if (config.action == "update") {
						var iframe = this.getIFrameEl();
						iframe.contentWindow.$.form.showData(config);
					}
				},
				ondestroy : function(action) {
					if (action&&action.indexOf("_") > 0) {
						var actions = action.split("_");
						var actionName = actions[0];
						var msg = actions[1];
						if (actionName == "save" && msg != "null" && msg != "") {
							tips.show(msg, '', 5000, '#DFF0D8', 150);
						}
					}
				}
			});
		}
	};
	var closeWindow = function(action) {
		if (action == "close" && form.isChanged()) {
			if (confirm("数据被修改了，是否先保存？")) {
				return false;
			}
		}
		if (window.CloseOwnerWindow)
			return window.CloseOwnerWindow(action);
		else
			window.close();
	};
	// 解析js对象为参数名=参数值方式
	var parseParam = function(config) {
		// 解析js对象为参数名=参数值方式
		var paramData = "";
		var iterParam = function(obj) {
			if (obj.constructor == Object) {
				var index = 0;
				for ( var i in obj) {
					if (index != 0) {
						paramData += "&";
					}
					if (obj[i] != null) {
						if (obj[i].constructor == Object) {
							iterParam(obj[i], i);
						} else {
							var value = obj[i];
							// 对value进行HTML字符转义
							value = escapeCharacter(value);
							if (value.constructor == Date) {// 日期转换
								value = mini.formatDate(value,
										"yyyy-MM-dd HH:mm:ss");
							}/* else {value = value.replace(/[&]/g,"&amp;");} */

							if (arguments.length == 2) {
								paramData += arguments[1] + "." + i + "="
										+ value;
							} else {
								paramData += i + "=" + value;
							}
						}
					}
					index++;
				}
			}
		};
		iterParam(config);
		return paramData;
	};

	function escapeCharacter(value) {
		if (typeof (value) == 'string') {// 特殊字符转义
			value = value.replace(/\%/g, '%25');
			value = value.replace(/\+/g, '%2B');
			value = value.replace(/\&/g, '%26');
		}
		return value;
	}

	$.extend(DataForm.prototype, {
		init : function() {
			mini.parse();
		},
		saveData : function(options) {
			var defaults = {
				url : "",
				id : "",
				validator : function() {
					return true;
				}
			};
			var opts = $.extend(defaults, options);
			var dataform = new mini.Form(opts.id);
			// 表单验证
			dataform.validate();
			if (dataform.isValid() == false) {
				return;
			}
			if (!opts.validator()) { // 业务验证
				return;
			}
			var data = dataform.getData(false, false); // 获取表单数据
			var filePaths = document.getElementsByName("filePath");// 附件上传
			var fileNames = document.getElementsByName("fileName");
			var attachmentOwners = document.getElementsByName("attachmentOwner");
			if (filePaths && filePaths.length > 0) {
				var filepath = [];
				var fileName = [];
				var attachmentOwner = [];
				for (var i = 0, a = filePaths.length; i < a; i++) {
					filepath.push(filePaths[i].value);
					fileName.push(fileNames[i].value);
					attachmentOwner.push(attachmentOwners[i].value);
				}
				data.filePaths = filepath.join(",");
				data.fileNames = fileName.join(",");
				data.attachmentOwners = attachmentOwner.join(",");
			}
			var paramData = parseParam(data);
			mini.mask({
				el : document.body,
				cls : 'mini-mask-loading',
				html : '正在提交数据,请稍后...'
			});
			$.ajax({
				url : opts.url,
				type : "post",
				data : paramData,
				cache : false,
				success : function(text) {// zhujiagui 修改，获取添加和修改成功与否的信息
						if (opts.showMsg) {
							closeWindow("save_" + text);
						} else
							closeWindow("save");
				},
				error : function(jqXHR, textStatus, errorThrown) {
					closeWindow();
				}
			});
		},
		saveOtherData : function(options) {
			var defaults = {
				url : "",
				id : "",
				validator : function() {
					return true;
				}
			};
			var opts = $.extend(defaults, options);
			var dataform = new mini.Form(opts.id);

			// 表单验证
			dataform.validate();
			if (dataform.isValid() == false) {
				return;
			}

			if (!opts.validator()) { // 业务验证
				return;
			}

			var data = dataform.getData(false, false); // 获取表单数据
			var filePaths = document.getElementsByName("filePath");// 附件上传
			var fileNames = document.getElementsByName("fileName");
			var attachmentOwners = document.getElementsByName("attachmentOwner");
			if (filePaths && filePaths.length > 0) {
				var filepath = [];
				var fileName = [];
				var attachmentOwner = [];
				for (var i = 0, a = filePaths.length; i < a; i++) {
					filepath.push(filePaths[i].value);
					fileName.push(fileNames[i].value);
					attachmentOwner.push(attachmentOwners[i].value);
				}
				data.filePaths = filepath.join(",");
				data.fileNames = fileName.join(",");
				data.attachmentOwners = attachmentOwner.join(",");
			}
			var paramData = parseParam(data);
			mini.mask({
				el : document.body,
				cls : 'mini-mask-loading',
				html : '正在提交数据,请稍后...'
			});
			$.ajax({
				url : opts.url,
				type : "post",
				data : {
					"data" : paramData,
					"ortherData" : opts.ortherData
				},
				cache : false,
				success : function(text) {// zhujiagui 修改，获取添加和修改成功与否的信息
					if (opts.showMsg) {
						closeWindow("save_" + text);
					} else
						closeWindow("save");
				},
				error : function(jqXHR, textStatus, errorThrown) {
					closeWindow();
				}
			});
		},
		onCancel : function() {
			closeWindow("cancel");
		},
		onClose : function() {
			if (window.CloseOwnerWindow)
				return window.CloseOwnerWindow("");
			else
				window.close();
		},
		showUpdate : function(config) {
			var form = new mini.Form(config.formId);
			var myurl = config.dataUrl;
			var param = config.fieldId + "=" + config.data.row[config.fieldId];
			if (myurl.indexOf("?") == -1) {
				myurl += "?" + param;
			} else {
				myurl += "&" + param;
			}
			$.ajax({
				url : myurl,
				cache : false,
				success : function(text) {
					// var o = mini.decode(text);//可将字符串日期转为data型日期
					form.setData(text);
					form.setChanged(false);
					$.form.loadFormSuccess(form);
				}
			});
		},
		add : function(options) {
			var data = {
				action : "new"
			};
			var defaults = {
				url : "",
				title : "添加",
				width : 600,
				height : 360
			};
			var opts = $.extend(defaults, options);
			opts.data = data;
			openWindow(opts);
		},
		edit : function(options) {
			var defaults = {
				url : "",
				dataUrl : "",
				fieldId : "id", // 获取选中行的主键值
				fieldIdValue : "1", // 获取选中行的主键值
				formId : "", // 表单Id属性值
				title : "显示数据",
				width : 600,
				height : 360
			};
			options.action = "update";
			var opts = $.extend(defaults, options);
			openWindow(opts);
		},
		showData : function(config) {
			var form = new mini.Form(config.formId);
			var myurl = config.dataUrl;
			var param = config.fieldId + "=" + config.fieldIdValue;
			if (myurl.indexOf("?") == -1) {
				myurl += "?" + param;
			} else {
				myurl += "&" + param;
			}
			$.ajax({
				url : myurl,
				cache : false,
				success : function(text) {
					// var o = mini.decode(text);//可将字符串日期转为data型日期
					form.setData(text);
					form.setChanged(false);
					$.form.loadFormSuccess(form);
				}
			});
		},
		loadFormSuccess : function() { // 表单数据加载完成后执行
		},
		parseParam : parseParam, // yxf 为DataForm添加参数解析函数
		closeWindow : closeWindow
	// yxf
	});

	$.form = new DataForm();
})(jQuery);